<template>
  <div class="form-container">
    <div class="form-wrapper">
      <el-form ref="registerForm" label-position="top" :model="registerForm" :rules="registerRules" status-icon>
        <el-form-item label="用户名" prop="username">
          <el-input placeholder="请输入用户名" v-model="registerForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input placeholder="请输入密码" v-model="registerForm.password" type="password" show-password autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirm_password">
          <el-input placeholder="请确认密码" v-model="registerForm.confirm_password" type="password" show-password autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width:100%;" @click="submitForm">立即注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { register } from '@/api/user'
import md5 from "md5"

export default {
  name: 'Register',
  data() {
    var validateUsername = (rule, value, callback) => {
      callback();
    }
    var validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度至少6位'));
      } else {
        callback();
      }

    }
    var validateConfirm = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次密码输入不一致'));
      } else {
        callback();
      }
    }
    return {
      registerForm: {
        username: null,
        password: null,
        confirm_password: null
      },
      registerRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { validator: validateUsername, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        confirm_password: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { validator: validateConfirm, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.resetForm()
  },
  methods: {
    resetForm() {
      this.registerForm = {
        username: null,
        password: null,
        confirm_password: null
      }
    },
    submitForm() {
      this.$refs['registerForm'].validate((valid) => {
        if (valid) {
          const password = this.registerForm.password
          this.registerForm.password = md5(password)
          register(this.registerForm).then(res => {
            if (res.code && res.code === 200) {
              this.$message({
                message: "注册成功",
                type: 'success',
                duration: 1000
              })
              this.$router.push({
                path: '/login'
              })
            } else {
              this.$message({
                message: res.msg,
                type: 'error',
                duration: 1000
              })
            }
          })
        }
      })
    },
  }
}
</script>
<style scoped>
.form-wrapper {
  width: 600px;
  margin: 100px auto;
  padding: 50px;
  border: 1px solid #C0C4CC;
  border-radius: 5px;
  background: #ffffff;
  opacity: 0.8;
}
.form-container {
  padding: 1px;
  background: url('../assets/img/background.png') no-repeat center fixed;     
  background-size: cover;
}
</style>
